package com.eshop.dao.Impl;

import com.eshop.dao.GoodsDao;
import com.eshop.model.Goods;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class GoodsDaoImpl extends BaseDao implements GoodsDao {
    @Override
    public List<Goods> queryGoodsByIsToday(int isToday) {
        String sql = "select * from t_goods where istoday = 1";
        return queryForList(Goods.class,sql);
    }

    @Override
    public Goods queryGoodsById(int goodsId) {
        String sql = "select * from t_goods where goods_id = ?";
        return queryForOne(Goods.class,sql,goodsId);
    }

    @Override
    public List<Goods> queryGoodsByCateId(int cateId) {
        String sql = "select * from t_goods where cate_id = ?";
        return queryForList(Goods.class,sql,cateId);
    }

    @Override
    public List<Goods> queryGoodsByCateIdSort(int cateId, Comparator<? super Goods> comparator) {
        String sql = "select * from t_goods where cate_id = ?";
        return queryForList(Goods.class,sql,cateId).stream().sorted(comparator).collect(Collectors.toList());
    }

    @Override
    public List<Goods> queryGoodsBySales(int num) {
        String sql = "select * from t_goods where goods_sales >= ?";
        return queryForList(Goods.class,sql,num);
    }

    @Override
    public List<Goods> queryGoodsByLatestDate() {
        String sql = "select * from t_goods where goods_date = (select max(goods_date) from t_goods)";
        return queryForList(Goods.class,sql);
    }

    @Override
    public List<Goods> queryGoodsByTop6Sales() {
        String sql = "select * from t_goods order by goods_sales desc limit 6";
        return queryForList(Goods.class,sql);
    }

    @Override
    public List<Goods> fuzzyEnquiry(String name) {
        String sql = "select * from t_goods where goods_name like ?";
        return queryForList(Goods.class,sql,"%"+name+"%");
    }

    @Override
    public List<Goods> fuzzyEnquirySort(String name, Comparator<? super Goods> comparator) {
        String sql = "select * from t_goods where goods_name like ?";
        return queryForList(Goods.class,sql,"%"+name+"%").stream().sorted(comparator).collect(Collectors.toList());
    }
}
